package com.myk.game.gobangchess.signs;

import com.myk.game.gobangchess.Sign;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 整个格子颜色背景标记
 */
public class ColorSign extends Sign {

    private final List<Integer> indexes;
    private final String color;

    public ColorSign(int index, String color) {
        this.indexes = new ArrayList<>();
        this.indexes.add(index);
        this.color = color;
    }

    public ColorSign(List<Integer> indexes, String color) {
        this.indexes = indexes;
        this.color = color;
    }

    private String idsToStr() {
        StringBuilder str = new StringBuilder();
        for (int index : indexes) {
            str.append(index).append("a");
        }
        str.deleteCharAt(str.length() - 1);
        return str.toString();
    }

    @Override
    public String getSignInfo() {
        return type + "," + idsToStr() + "," + color;
    }

    public void serialize(DataOutputStream dos) throws IOException {
        dos.writeInt(indexes.size());
        for (Integer index : indexes) {
            dos.writeInt(index);
        }
        dos.writeUTF(this.color);
    }

    public ColorSign(DataInputStream dis) throws IOException {
        int indexSize = dis.readInt();
        this.indexes = new ArrayList<>(indexSize);
        for (int i = 0; i < indexSize; i++) {
            this.indexes.add(dis.readInt());
        }
        this.color = dis.readUTF();
    }
}
